#!/bin/bash
################
### 需要sudo权限
################
set -xeE
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
PS4=$GREEN' [$0:$LINENO] '$NC
trap 'echo -e "$RED ERROR: [$0:$LINENO] $BASH_COMMAND $NC"' ERR
function print_info() {
    echo -e "$BLUE$*$NC"
}

DEPLOY_TOKEN_MOBILE_USER=ci_deploy_token_mobile
DEPLOY_TOKEN_MOBILE_TOKEN=wfjg-CmFD9PkftXzWX6c
DOCKER_IMAGE_PATH_PUBLISH="$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG"
DOCKER_IMAGE_PATH_COMMITS="$CI_REGISTRY_IMAGE/commits:$CI_COMMIT_SHORT_SHA"
DOCKER_IMAGE_TAGS=(
    $DOCKER_IMAGE_PATH_COMMITS
    $DOCKER_IMAGE_PATH_PUBLISH
    ${DOCKER_IMAGE_PATH_PUBLISH/repo1.antiy.cn:5443/harbor.antiy.cn}
)


function publish() {
    test "${CI_JOB_TOKEN}" == '' || {
        echo "$CI_REGISTRY"
        echo $DOCKER_IMAGE_PATH_COMMITS
        docker_image=${DOCKER_IMAGE_TAGS[0]}
        echo $docker_image
        docker login -u $DEPLOY_TOKEN_MOBILE_USER -p "$DEPLOY_TOKEN_MOBILE_TOKEN" $CI_REGISTRY
        docker pull "$docker_image"
        for i in "${DOCKER_IMAGE_TAGS[@]:1}"; do
            echo $i
            docker rmi $i || true
            docker tag $docker_image $i
            docker push $i
        done
    }
}
function main() {
    publish
}
main